package com.lvcoding.cloud.user.conroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lvcoding.cloud.api.entity.SysUser;
import com.lvcoding.cloud.common.core.response.Res;
import com.lvcoding.cloud.api.feign.OrderFeign;
import com.lvcoding.cloud.common.security.auth.CommonUser;
import com.lvcoding.cloud.common.security.util.SecurityUtil;
import com.lvcoding.cloud.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author wuyanshen
 * @discription 用户控制器
 * @date 2020-07-13 3:17 下午
 */
@RestController
@RefreshScope
@RequestMapping("user")
public class UserController {

    @Value("${author.username}")
    private String name;

    @Value("${version}")
    private String version;

    @Value("${scope}")
    private String scope;

    @Value("${server.port}")
    private String port;

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取nacos中的配置
     * user-dev.yml
     */
    @GetMapping("nacos")
    public String nacos() {
        return scope + "-" + version + "-" + port;
    }


    /**
     * 调用order接口
     *
     * @return
     */
    @GetMapping("nacos/order")
    public String orderNacos() {
        return orderFeign.nacos();
    }

    /**
     * 根据用户名查询用户
     *
     * @return
     */
    @GetMapping("info/{username}")
    public Res<SysUser> userInfo(@PathVariable("username") String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().ge(SysUser::getUsername, username);
        SysUser sysUser = sysUserService.getOne(wrapper);
        return Res.success(sysUser);
    }

    @GetMapping("info/user")
    public Res<CommonUser> currentUser(){
        CommonUser commonUser = SecurityUtil.getCurrentLoginUser();
        return Res.success(commonUser);
    }
}
